package com.yycwxt.modules.admin.express;

import com.yycwxt.common.ReturnResult;
import com.yycwxt.modules.admin.data_import.AccountData;
import com.yycwxt.modules.admin.data_import.ImpFile;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.UUID;

@Api
@RestController
@Validated
@RequestMapping("/express")
public class ExpressController {

    private final ExpressService expressService;

    public ExpressController(@Autowired ExpressService expressService){
        this.expressService = expressService;
    }

    //根据期次查询数据
//    @GetMapping("/select")
//    @ApiOperation("根据期次进行查询")
//    public ReturnResult select(@Validated @ApiParam(name = "期次", required = true, type = "String")  String zq){
//        return ReturnResult.buildSuccessResult(expressService.select(zq));
//    }

    //根据存储过程插入数值
//    @GetMapping("/save")
//    @ApiOperation("调用存储过程插入数据")
//    public ReturnResult save(@Validated @ApiParam(name = "期次值", required = true, type = "String") String zq){
//        expressService.save(zq);
//        return ReturnResult.buildSuccessResult(null);
//    }

    //查询账期
    @GetMapping("/selectZq")
    @ApiOperation("查询账期")
    public ReturnResult selectZq(){
        return ReturnResult.buildSuccessResult(expressService.selectZq());
    }

    //excel导入
//    @PostMapping("/upload")
//    @ApiOperation("Excel导入数据")
//    public ReturnResult excelToDatabase(@ApiParam(value = "上传文件", type = "_file") @RequestPart("file") MultipartFile file) {
//        try {
//            if (!file.isEmpty()) {
//                if (expressService.importFileToDB(true, file)) {
//                    return ReturnResult.buildSuccessResult("导入成功", null);
//                } else {
//                    return ReturnResult.buildFailureResult("导入失败", null);
//                }
//            } else {
//                return ReturnResult.buildFailureResult("文件为空", null);
//            }
//        }
//        catch (Exception e){
//            return ReturnResult.buildFailureResult("不支持此文件类型 或 文件内容格式不对", null);
//        }
//    }

    //查询快递费表格数据
    @GetMapping("/index")
    @ApiOperation("查询快递费数据表格")
    public ReturnResult greet(@Validated @ApiParam(name = "期次值", required = true, type = "String") String zq) {
        return ReturnResult.buildSuccessResult(expressService.selectKdf(zq));
    }

    //下载源文件查询url
    @GetMapping("/selectUrl")
    @ApiOperation("查询下载文件的url")
    public ReturnResult selectUrl(){
        return ReturnResult.buildSuccessResult(expressService.selectUrl());
    }

    @Value("${file.location}")
    String filelocation;
    @Value("${file.path}")
    String filepath;
    //上传快递费对账单
    @PostMapping("/Kdfdzd")
    @ApiOperation("快递费对账单上传")
    public ReturnResult FileUpLoad(@ApiParam(value = "快递费对账单excel文件", type = "_file") @RequestPart("file") MultipartFile file) {
        if (file.isEmpty()) {
            return ReturnResult.buildFailureResult("文件为空");
        } else {
            try {
                // 保存文件
                // 服务器文件名
                String serverFilename = UUID.randomUUID().toString().substring(0, 8) + ".xlsx";
                // 创建目录
                String path = filelocation + AccountData.getAccountDate();
                System.out.println(path);
                Files.createDirectories(Paths.get(path));
                // 保存
                file.transferTo(Path.of(path, serverFilename));

                // 记录上传文件明细
                ImpFile impFile = new ImpFile();
                impFile.setFilename(file.getOriginalFilename());
                impFile.setName("快递费对账单");
                impFile.setUrl("download/" + AccountData.getAccountDate() + "/" + serverFilename);
                impFile.setZq(AccountData.getAccountDate());
                impFile.setCreated(LocalDateTime.now());
                impFile.setRemark("快递费用");
                System.out.println(impFile);
                expressService.save(impFile);

                // 解析excel，记录流水
                expressService.importKdfdzd(file);
                return ReturnResult.buildSuccessResult("导入成功", true);
            } catch (Exception e) {
                e.printStackTrace();
                return ReturnResult.buildFailureResult(e.toString());
            }
        }
    }
}
